﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post;
using sexhealth.console.service.post.vo;
using System.Text;
using com.pro2e.web.util.page;

public partial class admin_UC02_0201postmaintain : System.Web.UI.Page
{
    PostService postService = new PostService();

    private int PostMode
    {
        get { if (ViewState["postmode"] == null) { ViewState["postmode"] = 0; } return int.Parse(ViewState["postmode"].ToString()); }
        set { ViewState["postmode"] = value; }
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        ddlChannel.Items.Add(new ListItem("請選擇", "-1"));
        foreach (ChannelVO ch in postService.GetChannelList(4))
        {
            ddlChannel.Items.Add(new ListItem(ch.Name, ch.ChannelNo.ToString()));
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showListMode();
            GetList();
        }
    }
    protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetList();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        PostMode = 0;
        showModifyMode();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        showListMode();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (PostMode == 0)
        {
            PostVO post = new PostVO();
            post.HtmlContent = ckbContent.value;
            post.Title = txtTitle.Text.Trim();
            if (txtOrder.Text != string.Empty)
            {
                post.OrderNo = int.Parse(txtOrder.Text.Trim());
            }
          
            post.KeyWord = txtKeyWord.Text;
            post.BelongToChannelId = int.Parse(ddlChannel.SelectedValue);
            postService.InsertPost(post);
            
        }
        else 
        {
            PostVO post = postService.DaoGetVOById<PostVO>(PostMode);
            post.Title = txtTitle.Text.Trim();
            if (txtOrder.Text != string.Empty)
            {
                post.OrderNo = int.Parse(txtOrder.Text.Trim());
            }
            post.HtmlContent = ckbContent.value;
            post.KeyWord = txtKeyWord.Text;
            postService.UpdatePost(post);
        }
        showListMode();
        GetList();

    }
    private void showListMode()
    {
        pnlGv.Visible = true;
        pnlContent.Visible = false;
        btnAddNew.Visible = true;
    }
    private void showModifyMode()
    {
        pnlGv.Visible = false;
        pnlContent.Visible = true;
        btnAddNew.Visible = false;
    }
    private void GetList()
    {
        if (ddlChannel.SelectedValue != "-1")
        {
            gvResult.DataSource = postService.GetPostList(int.Parse(ddlChannel.SelectedValue));
            gvResult.DataBind();
        }
    }
    protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int PostNo = int.Parse(e.CommandArgument.ToString());
        PostVO post = postService.DaoGetVOById<PostVO>(PostNo);
        switch (cmdName)
        {
            case "myModify":
             
                txtTitle.Text = post.Title;
                ckbContent.value = post.HtmlContent;
                txtKeyWord.Text = post.KeyWord;
                txtOrder.Text = post.OrderNo.ToString();
                showModifyMode();
                PostMode = PostNo;
                break;

            case "myDel":
                postService.DaoDelete(post);
                GetList();
                break;
       
            default:
                break;
        }
    }
   
}
